<?php

class Tourism_order_refund_model extends CI_Model
{
    private $table = 'tourism_order_refund';
    private $table_1 = 'tourism_order';
    private $table_2 = 'tourism_order_product';
    private $table_3 = 'user';

    /*
     *退款申请记录添加
     * */
    public function insert($postData, $imageData)
    {
        $data = array(
            'order_id'         => $postData['order_id'],
            'goods_attr_id'    => $postData['goods_attr_id'],
            'order_product_id' => $postData['order_product_id'],
            'existing'         => $postData['existing'],
            'number'           => $postData['number'],
            'seller_uid'       => $postData['seller_uid'],
            'uid'              => $postData['uid'],
            'user_name'        => $postData['user_name'],
            'cellphone'        => $postData['cellphone'],
            'counter_fee'      => 0,
            'status'           => 1,
            'logistics_company'=> '',
            'logistics'        => '',
//            'drawback'         => $postData['drawback'],
            'flag'             => 1,
            'refund_content'   => $postData['refund_content'],
            'reject_content'   => '',
            'created_at'       => date('Y-m-d H:i:s'),
            'verify_time'      => '0000-00-00 00:00:00'
        );

        if(is_array($imageData)){
            $data['images'] = $imageData['file_name'];
        }else{
            $data['images'] = $imageData;
        }
        return $this->db->insert($this->table, $data);
    }

    public function findByOrderProductId($id)
    {
        $this->db->select('tourism_order_refund.*,goods_image,pay_amount,goods_name');
        $this->db->from($this->table.' AS tourism_order_refund');
        $this->db->join($this->table_2.' AS tourism_order_product', 'tourism_order_refund.order_product_id=tourism_order_product.order_product_id','inner');
        $this->db->where('tourism_order_refund.refund_id', $id);
        $this->db->order_by('refund_id', 'DESC');
        $this->db->limit(1);
        return $this->db->get();
    }

    /*
     *判断是否存在未完成的退款
     * */
    public function getRefundNum($order_id)
    {
        $this->db->where('order_id',$order_id);
        $this->db->where('status',1);
        return $this->db->count_all_results($this->table);
    }

    public function findRefundList($uid){
        $this->db->select('tourism_order_refund.refund_id,existing,tourism_order_refund.user_name,tourism_order_refund.status,tourism_order_refund.flag,,tourism_order_refund.number,
                            tourism_order_product.goods_name,attribute_value,goods_image,shop_price,pay_amount,
                            user.alias_name,counter_fee');
        $this->db->from($this->table.' as tourism_order_refund');
        $this->db->join($this->table_2.' as tourism_order_product','tourism_order_product.order_product_id = tourism_order_refund.order_product_id');
        $this->db->join($this->table_3.' as user','tourism_order_refund.seller_uid = user.uid');
        $this->db->where('tourism_order_refund.uid',$uid);
//        $this->db->limit(1);
        $this->db->order_by('refund_id', 'DESC');
//        $this->db->group_by('tourism_order_refund.order_id');
        return $this->db->get();
    }

    /**
     * 退款总页数
     */
    public function refundPageTotal($param)
    {
        $this->db->select('tourism_order_refund.refund_id');
        $this->db->from($this->table.' as tourism_order_refund');
        $this->db->join($this->table_2.' as tourism_order_product','tourism_order_product.order_product_id = tourism_order_refund.order_product_id');
        $this->db->join($this->table_3.' as user','tourism_order_refund.seller_uid = user.uid');
        $this->db->where('tourism_order_refund.uid',$param['uid']);
        return $this->db->count_all_results();
    }

    /**
     * 退款列表
     */
    public function refundList($param, $count, $start = 0)
    {
        $this->db->select('tourism_order_refund.refund_id,existing,tourism_order_refund.user_name,tourism_order_refund.status,tourism_order_refund.flag,,tourism_order_refund.number,
                            tourism_order_product.goods_name,attribute_value,goods_image,shop_price,pay_amount,
                            user.alias_name,counter_fee');
        $this->db->from($this->table.' as tourism_order_refund');
        $this->db->join($this->table_2.' as tourism_order_product','tourism_order_product.order_product_id = tourism_order_refund.order_product_id');
        $this->db->join($this->table_3.' as user','tourism_order_refund.seller_uid = user.uid');
        $this->db->where('tourism_order_refund.uid', $param['uid']);
        $this->db->order_by('refund_id', 'DESC');
        $this->db->limit($count, $start);
        return $this->db->get();
    }
    
    /**
     * @descripte
     * @Author xiumao
     * @date 2016/6/17 0017 上午 11:46
     */
    public function selectByOrderProductId($order_product_id)
    {
        $this->db->where('order_product_id', $order_product_id);
        return $this->db->count_all_results($this->table);
    }

}